import express from 'express';
import compress from 'compression';
import bodyParser from 'body-parser';
import { extendAPIOutput } from '../middlewares/api_output';
import { limitAccessBeyondMicroMessenger,checkApiAndToken } from '../middlewares/interceptor';
import baidu from './baidu';
import common from './common';
import thirdApi from './third_api';
import wx from './wx';
import {APP_VERSION_CODE} from '../config/config'; 

// 注册每个模块的根路由
export default (app) => {

    //  ---HTTP 配置----
    app.use(bodyParser.json({ limit: '500kb' }));
    app.use(bodyParser.text({ type: 'text/xml' }));
    app.use(bodyParser.urlencoded({ extended: true }));

    app.use(function (request, response, next) {
        console.log(request.url, JSON.stringify(request.body));
        response.header("Access-Control-Allow-Origin", "*.aidoor.net");  
        response.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
        response.header("Access-Control-Allow-Headers", "X-Requested-With");
        response.header('Access-Control-Allow-Headers', 'Content-Type');
        next();
    });

    // 标准化输出
    app.use(extendAPIOutput);

    // 仅微信服务器调用接口
    app.use('', wx);

    // 设备调用接口以及百度验证token接口
    app.use('/web_api', thirdApi);

    // 限制微信以外的访问
    // app.use(limitAccessBeyondMicroMessenger);
   
    // 前端网页访问
    app.use(compress());
    app.use(express.static('web'));
    app.use([
        `/story_${APP_VERSION_CODE}/`,
        `/mydevice_${APP_VERSION_CODE}/`,
        '/wxbind',
        '/scanbind/',
        '/soundbind/',
        '/unbind/',
        '/share_device/:ticket',
        '/upgrade/:version/:uuid'
    ], express.static('web/story'));

    // 对接口进行Token校验
    // app.use(checkApiAndToken);

    // 访问百度服务器接口
    app.use('/web_api/baidu', baidu);

    // 通用的业务逻辑接口
    app.use('/web_api', common);

}
